package com.fr.web.face;

import com.fr.base.FRContext;
import com.fr.base.XMLable;
import com.fr.base.core.FRCoreContext;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONObject;
import com.fr.base.file.FaceManager;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLReadable;
import com.fr.base.xml.XMLableReader;
import com.fr.privilege.authentication.Authentication;
import com.fr.privilege.vote.VoteManager;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;

public class FaceNode
  implements XMLable
{
  private NodeDef nodeDef;
  private List faceNodeList = new ArrayList();

  public JSONObject toJSONConfig(HttpServletRequest paramHttpServletRequest, Authentication paramAuthentication, FaceManager paramFaceManager, String paramString, int paramInt)
    throws Exception
  {
    Object localObject;
    if (!(VoteManager.vote(paramAuthentication, this.nodeDef)))
      return null;
    JSONObject localJSONObject1 = new JSONObject();
    localJSONObject1.put("id", paramString + paramInt);
    if (this.nodeDef != null)
    {
      localJSONObject1.put("text", this.nodeDef.getName());
      localObject = this.nodeDef.createURI(paramHttpServletRequest, paramAuthentication);
      if (((Access)localObject).isAccess())
      {
        localJSONObject1.put("value", ((Access)localObject).getUrl());
        break label123:
      }
      return null;
    }
    localJSONObject1.put("value", "undefined");
    label123: localJSONObject1.put("complete", true);
    localJSONObject1.put("isexpand", false);
    localJSONObject1.put("checkstate", 0);
    if (getChildCount() > 0)
    {
      localJSONObject1.put("hasChildren", true);
      localObject = new JSONArray();
      localJSONObject1.put("ChildNodes", localObject);
      int i = 0;
      while (true)
      {
        if (i >= getChildCount())
          break label267;
        FaceNode localFaceNode = getChild(i);
        JSONObject localJSONObject2 = localFaceNode.toJSONConfig(paramHttpServletRequest, paramAuthentication, paramFaceManager, paramString + paramInt, i + 1);
        if (localJSONObject2 != null)
          ((JSONArray)localObject).put(localJSONObject2);
        ++i;
      }
    }
    localJSONObject1.put("leaf", true);
    label267: return ((JSONObject)localJSONObject1);
  }

  public NodeDef getNodeDef()
  {
    return this.nodeDef;
  }

  public void setNodeDef(NodeDef paramNodeDef)
  {
    this.nodeDef = paramNodeDef;
  }

  public void addChild(FaceNode paramFaceNode)
  {
    this.faceNodeList.add(paramFaceNode);
  }

  public int getChildCount()
  {
    return this.faceNodeList.size();
  }

  public FaceNode getChild(int paramInt)
  {
    return ((FaceNode)this.faceNodeList.get(paramInt));
  }

  public void clearChildren()
  {
    this.faceNodeList.clear();
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if (paramXMLableReader.isAttr())
      clearChildren();
    if (paramXMLableReader.isChildNode())
    {
      Object localObject;
      String str2 = paramXMLableReader.getTagName();
      if ("FaceNode".equals(str2))
      {
        localObject = new FaceNode();
        addChild((FaceNode)localObject);
        paramXMLableReader.readXMLObject((XMLReadable)localObject);
      }
      else if ("NodeDef".equals(str2))
      {
        String str1;
        if ((str1 = paramXMLableReader.getAttr("class")) != null)
        {
          localObject = null;
          if (str1.endsWith(".ReportletDef"))
            localObject = new ViewReportletDef();
          else if (str1.endsWith(".ViewReportletDef"))
            localObject = new ViewReportletDef();
          else if (str1.endsWith(".WriteReportletDef"))
            localObject = new WriteReportletDef();
          else if (str1.endsWith(".WebURLDef"))
            localObject = new WebURLDef();
          else if (str1.endsWith(".FolderDef"))
            localObject = new FolderDef();
          else
            try
            {
              localObject = (NodeDef)FRCoreContext.classForName(str1).newInstance();
            }
            catch (Exception localException)
            {
              FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
            }
          if (localObject != null)
          {
            paramXMLableReader.readXMLObject((XMLReadable)localObject);
            setNodeDef((NodeDef)localObject);
          }
        }
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("FaceNode");
    if (this.nodeDef != null)
    {
      paramXMLPrintWriter.startTAG("NodeDef").attr("class", this.nodeDef.getClass().getName());
      this.nodeDef.writeXML(paramXMLPrintWriter);
      paramXMLPrintWriter.end();
    }
    int i = getChildCount();
    for (int j = 0; j < i; ++j)
    {
      FaceNode localFaceNode = getChild(j);
      localFaceNode.writeXML(paramXMLPrintWriter);
    }
    paramXMLPrintWriter.end();
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    return super.clone();
  }
}